package com.itheima.server.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.server.service.MovementsService;
import com.itheima.tanhua.model.domain.PageResult;
import com.itheima.tanhua.model.mongo.Movement;
import com.itheima.tanhua.model.vo.MovementsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/movements")
public class MovementsController {

    @Autowired
    private MovementsService movementsService;


    /**
     * 发布动态
     * @param movement
     * @param imageContent
     * @return
     * @throws IOException
     */
    @PostMapping
    public ResponseEntity movements(Movement movement,
                                    MultipartFile imageContent[]) throws IOException {
        movementsService.publishMovements(movement,imageContent);

        return ResponseEntity.ok(null);
    }

    /**
     * 查询我的动态
     * @param page
     * @param pagesize
     * @param userId
     * @return
     */
    @GetMapping("/all")
    public ResponseEntity findAll(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "4")Integer pagesize,Long userId){

        PageResult pageResult = movementsService.findByUserId(page,pagesize,userId);

        return ResponseEntity.ok(pageResult);
    }

    /**
     * 好友动态
     * @return
     */
    @GetMapping
    public ResponseEntity movements(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "4") Integer pagesize){

        PageResult pageResult = movementsService.findFriendMovements(page,pagesize);
        return ResponseEntity.ok(pageResult);
    }

    @GetMapping("/recommend")
    public ResponseEntity recommend(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "4") Integer pagesize){

        PageResult pageResult = movementsService.findRecommendMovements(page,pagesize);
        return ResponseEntity.ok(pageResult);
    }

    @GetMapping("/{id}")
    public ResponseEntity findByMovmentId(@PathVariable String id){
       MovementsVo movementsVo = movementsService.findByMovmentId(id);

       return ResponseEntity.ok(movementsVo);
    }
}
